home *** CD-ROM | disk | FTP | other *** search
- /* StylDialog demo by Ingemar Ragnemalm 1995 */
- /* Based on a help system by Cary Torkelson */
-
-
- #include <QDOffScreen.h>
-
- void ShowStyledTextDialog(short textResourceID);
-
-
- short theType;
- short itemHit;
- Handle itemhdl;
- Rect itembox;
- DialogPtr theDialog;
- TEHandle styledTextTEHandle;
- Rect textRect;
- ControlHandle theScrollbar;
- short numberLinesPerPage;
-
-
- static void ScrollTextToScrollValue()
- {
- short oldScroll;
- short newScroll;
- short scrollbarValue;
-
- HLock((Handle)styledTextTEHandle);
-
- oldScroll = (**styledTextTEHandle).viewRect.top - (**styledTextTEHandle).destRect.top;
- scrollbarValue = GetCtlValue(theScrollbar);
- if ( scrollbarValue == 0 )
- newScroll = 0;
- else
- newScroll = TEGetHeight(GetCtlValue(theScrollbar), 0, styledTextTEHandle);
- TEScroll(0, oldScroll - newScroll, styledTextTEHandle);
-
- HUnlock((Handle)styledTextTEHandle);
- } /*ScrollTextToScrollValue*/
-
-
- static pascal void ScrollText(ControlHandle theControl, short thePart)
- {
- short delta;
- short oldValue;
-
- switch ( thePart )
- {
- case inUpButton:
- delta = -1;break;
- case inDownButton:
- delta = 1;break;
- case inPageUp:
- delta = -numberLinesPerPage;break;
- case inPageDown:
- delta = numberLinesPerPage;break;
- default: ;
- }
- if ( thePart != 0 )
- {
- oldValue = GetCtlValue(theControl);
- SetCtlValue(theControl, oldValue + delta);
- ScrollTextToScrollValue();
- }
- } /*ScrollText*/
-
-
- static pascal Boolean MyDialogFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
- Point clickPoint;
- Boolean returnValue;
- short thePart;
- ControlHandle theControl;
- short chcode;
-
- returnValue = false;
- if ( theEvent->what == mouseDown )
- {
- clickPoint = theEvent->where;
- GlobalToLocal(&clickPoint);
- thePart = FindControl(clickPoint, theDialog, &theControl);
- if ( theControl == theScrollbar )
- if ( thePart == inThumb )
- {
- thePart = TrackControl(theScrollbar, clickPoint, nil);
- ScrollTextToScrollValue();
- }
- else
- thePart = TrackControl(theScrollbar, clickPoint, &ScrollText);
- }
- else if ( theEvent->what == keyDown )
- {
- chcode = (theEvent->message & charCodeMask);
- if ( (chcode == 13) || (chcode == 3) )
- {
- *itemHit = ok;
- returnValue = true;
- }
- }
- return returnValue;
- } /*MyDialogFilter*/
-
-
- void ShowStyledTextDialog(short textResourceID)
- {
- #define kHelpID 128
- #define kHelpTextTEID 2
- #define kScrollBarID 4
-
- Rect tempRect;
- Handle textResourceHandle;
- StScrpHandle stylResourceHandle;
- short maxScrollValue;
-
- /*Get the dialog*/
- theDialog = GetNewDialog(kHelpID, 0L, (WindowPtr)-1);
- SetPort(theDialog);
- /*Get the user item defining where the text goes.*/
- GetDItem(theDialog, kHelpTextTEID, &theType, &itemhdl, &textRect);
- /*Get the scrollbar handle*/
- GetDialogItem(theDialog, kScrollBarID, &theType, (Handle *) &theScrollbar, &tempRect);
- /*Make a TextEdit record*/
- styledTextTEHandle = TEStylNew(&textRect, &textRect);
- /*Get the 'TEXT'/'styl' resources and insert them into the TextEdit record.*/
- textResourceHandle = GetResource('TEXT', textResourceID);
- stylResourceHandle = (StScrpHandle)GetResource('styl', textResourceID);
- HLock(textResourceHandle);
- TEStylInsert(*textResourceHandle, SizeResource(textResourceHandle), stylResourceHandle, styledTextTEHandle);
- HUnlock(textResourceHandle);
- ReleaseResource(textResourceHandle);
- ReleaseResource((Handle)stylResourceHandle);
- /*Set scrollbar values depending on the text*/
- numberLinesPerPage = (textRect.bottom - textRect.top) / (TEGetHeight((**styledTextTEHandle).nLines, 0, styledTextTEHandle) / (**styledTextTEHandle).nLines);
- maxScrollValue = (**styledTextTEHandle).nLines - numberLinesPerPage;
- if ( (maxScrollValue <= 0) )
- HiliteControl(theScrollbar, 255);
- else
- SetCtlMax(theScrollbar, maxScrollValue);
- /*Frame the text area and redraw it*/
- tempRect = textRect;
- InsetRect(&tempRect, -1, -1);
- FrameRect(&tempRect);
- TEUpdate(&textRect, styledTextTEHandle);
-
- InitCursor ();
- /*Run ModalDialog until the user clicks OK*/
- itemHit = 0;
- while (itemHit != ok)
- ModalDialog(&MyDialogFilter, &itemHit);
-
- DisposeControl(theScrollbar);
- DisposeDialog(theDialog);
- TEDispose(styledTextTEHandle);
- } /*ShowStyledTextDialog*/
-
-
- /* Standard inits */
-
- static void InitToolbox(void) {
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent,0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (nil);
- InitCursor ();
- } /*InitToolbox*/
-
-
- /* Main program */
-
- void main(void)
- {
- InitToolbox();
-
- ShowStyledTextDialog(128);
- } /*main*/
-